package com.ztf0337.mycurriculumdesign.ui.manager;

import static android.view.View.GONE;
import static android.view.View.VISIBLE;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.ztf0337.mycurriculumdesign.R;
import com.ztf0337.mycurriculumdesign.adapter.OrderAdapter;
import com.ztf0337.mycurriculumdesign.entity.Order;
import com.ztf0337.mycurriculumdesign.service.OrderService;
import com.ztf0337.mycurriculumdesign.ui.activity.OrderDetailActivity;

import java.util.List;

public class MOrderFragment extends Fragment {
    private Context context;
    private RecyclerView rvOrder;
    private OrderService orderService;
    private OrderAdapter adapter;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater,
                             @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_order, container, false);
        context = getContext();
        if (context == null) {
            return view;
        }


        ImageView ic_add;
        ic_add = requireActivity().findViewById(R.id.ic_add);
        ic_add.setVisibility(GONE);

        rvOrder = view.findViewById(R.id.rvOrder);
        rvOrder.setLayoutManager(new LinearLayoutManager(getContext()));
        orderService = new OrderService(context);
        TextView notOrder = view.findViewById(R.id.notOrder);
        List<Order> orderList = orderService.getAll();
        if (!orderList.isEmpty()) {
            notOrder.setVisibility(GONE);
            adapter = new OrderAdapter(orderList);
            rvOrder.setAdapter(adapter);
            adapter.setOnItemClickListener((order) -> {
                Intent intent = new Intent(context, OrderDetailActivity.class);
                intent.putExtra("id", order.getId());
                context.startActivity(intent);
            });
        } else {
            notOrder.setText("无订单信息");
            notOrder.setVisibility(VISIBLE);
        }

        ImageView ic_Search = requireActivity().findViewById(R.id.ic_Search);
        EditText etSearch = requireActivity().findViewById(R.id.etSearch);
        etSearch.setText(""); // 清空内容

        etSearch.setHint("请输入车位号或订单号");
        ic_Search.setOnClickListener(v -> {
            String search = etSearch.getText().toString().trim();
            if (search.isBlank()) {
                if (orderList.isEmpty()) {
                    Toast.makeText(context, "无订单信息", Toast.LENGTH_SHORT).show();
                } else {
                    adapter.updateData(orderList);
                }
                return;
            }
            List<Order> orders = orderService.searchAllOrders(search);
            if (orders.isEmpty()) {
                Toast.makeText(context, "无符合条件的订单", Toast.LENGTH_SHORT).show();
            } else {
                adapter.updateData(orders);
            }
        });

        return view;
    }
}
